#coding=utf-8
#Boa:Frame:FramePeer

import wx

import DlgCustomerEditor 
from product import CDataCustomer,CDataSupplier
from ctrlAider import *
from config import *

def create(parent):
    return FramePeer(parent)

[wxID_FRAMEPEER, wxID_FRAMEPEERBTNADD, wxID_FRAMEPEERBTNADDSUPPLIER, 
 wxID_FRAMEPEERBTNDEL, wxID_FRAMEPEERBTNDELSUPPLIER, wxID_FRAMEPEERBTNEDIT, 
 wxID_FRAMEPEERBTNEDITSUPPLIER, wxID_FRAMEPEERNOTEBOOK_PEER, 
 wxID_FRAMEPEERPANEL_CUSTOMER, wxID_FRAMEPEERPANEL_SUPPLIER, 
] = [wx.NewId() for _init_ctrls in range(10)]

class FramePeer(wx.Frame):
    def _init_coll_notebook_peer_Pages(self, parent):
        # generated method, don't edit

        parent.AddPage(imageId=-1, page=self.panel_customer, select=True,
              text=u'\u5ba2\u6237\u8bbe\u7f6e')
        parent.AddPage(imageId=-1, page=self.panel_supplier, select=False,
              text=u'\u4f9b\u5e94\u5546\u8bbe\u7f6e')

    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAMEPEER, name=u'FramePeer',
              parent=prnt, pos=wx.Point(262, 2), size=wx.Size(1018, 766),
              style=wx.DEFAULT_FRAME_STYLE, title=u'\u5546\u4e1a\u4f19\u4f34')
        self.SetClientSize(wx.Size(1010, 732))
        self.Center(wx.HORIZONTAL)
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.SetBackgroundColour(wx.Colour(183, 209, 173))
        self.Bind(wx.EVT_CLOSE, self.OnFramePeerClose)

        self.notebook_peer = wx.Notebook(id=wxID_FRAMEPEERNOTEBOOK_PEER,
              name=u'notebook_peer', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(1010, 732), style=0)

        self.panel_customer = wx.Panel(id=wxID_FRAMEPEERPANEL_CUSTOMER,
              name=u'panel_customer', parent=self.notebook_peer, pos=wx.Point(0,
              0), size=wx.Size(1002, 703), style=wx.TAB_TRAVERSAL)

        self.panel_supplier = wx.Panel(id=wxID_FRAMEPEERPANEL_SUPPLIER,
              name=u'panel_supplier', parent=self.notebook_peer, pos=wx.Point(0,
              0), size=wx.Size(1002, 703), style=wx.TAB_TRAVERSAL)

        self.btnAdd = wx.Button(id=wxID_FRAMEPEERBTNADD, label=u'\u65b0\u589e',
              name=u'btnAdd', parent=self.panel_customer, pos=wx.Point(64, 24),
              size=wx.Size(75, 24), style=0)
        self.btnAdd.Bind(wx.EVT_BUTTON, self.OnBtnAddButton,
              id=wxID_FRAMEPEERBTNADD)

        self.btnDel = wx.Button(id=wxID_FRAMEPEERBTNDEL, label=u'\u5220\u9664',
              name=u'btnDel', parent=self.panel_customer, pos=wx.Point(240, 24),
              size=wx.Size(75, 24), style=0)
        self.btnDel.Show(False)
        self.btnDel.Bind(wx.EVT_BUTTON, self.OnBtnDelButton,
              id=wxID_FRAMEPEERBTNDEL)

        self.btnEdit = wx.Button(id=wxID_FRAMEPEERBTNEDIT,
              label=u'\u4fee\u6539', name=u'btnEdit',
              parent=self.panel_customer, pos=wx.Point(152, 24),
              size=wx.Size(75, 24), style=0)
        self.btnEdit.Bind(wx.EVT_BUTTON, self.OnBtnEditButton,
              id=wxID_FRAMEPEERBTNEDIT)

        self.btnAddSupplier = wx.Button(id=wxID_FRAMEPEERBTNADDSUPPLIER,
              label=u'\u65b0\u589e', name=u'btnAdd', parent=self.panel_supplier,
              pos=wx.Point(64, 24), size=wx.Size(75, 24), style=0)
        self.btnAddSupplier.Bind(wx.EVT_BUTTON, self.OnBtnAddSupplierButton,
              id=wxID_FRAMEPEERBTNADDSUPPLIER)

        self.btnDelSupplier = wx.Button(id=wxID_FRAMEPEERBTNDELSUPPLIER,
              label=u'\u5220\u9664', name=u'btnDel', parent=self.panel_supplier,
              pos=wx.Point(240, 24), size=wx.Size(75, 24), style=0)
        self.btnDelSupplier.Show(False)

        self.btnEditSupplier = wx.Button(id=wxID_FRAMEPEERBTNEDITSUPPLIER,
              label=u'\u4fee\u6539', name=u'btnEdit',
              parent=self.panel_supplier, pos=wx.Point(152, 24),
              size=wx.Size(75, 24), style=0)
        self.btnEditSupplier.Bind(wx.EVT_BUTTON, self.OnBtnEditSupplierButton,
              id=wxID_FRAMEPEERBTNEDITSUPPLIER)

        self._init_coll_notebook_peer_Pages(self.notebook_peer)

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.SetSize(Cfg.getDlgAreaSize())
        self.Move(Cfg.getDlgAreaPos())
        
        #self.custCols = ['id','name','linkman','lm_phone', 'debt', 'addr','email','fax','memo']
##        for i in range(len(self.custCols)):            
##            n = CDataCustomer.getFieldNo(self.custCols[i])
##            cnName = CDataCustomer.m_fieldCNNames[n]
##            self.listCtrl_customer.InsertColumn(i, cnName)
        #CListCtrlAider.initListCtrlCols(self.listCtrl_customer, self.custCols, CDataCustomer)  
         
        #CDataCustomer.refreshDataList()
        
        #self.refreshViewCustomerList()
        
        self.myInit()
        
        
    def myInit(self):
        import MyListCtrl
        # supplier List
        self.lcSupplier = MyListCtrl.MySupplierListCtrl(self.panel_supplier)
        self.lcSupplier.Move(wx.Point(30, 60))
        self.lcSupplier.SetSize((1000, 500))
        self.lcSupplier.refresh()
               
        self.lcCust = MyListCtrl.MyCustomerListCtrl(self.panel_customer)
        self.lcCust.setViewPeerAll()
        self.lcCust.SetSize(wx.Size(1000, 500))
        self.lcCust.Move(wx.Point(30, 60))
        self.lcCust.refresh()
        
##    def refreshViewList(self, listview, cols, cls):
##        listview.DeleteAllItems() 
##        for obj in cls.m_dataList:
##            row = listview.InsertStringItem(sys.maxint, '')
##            for i in range(len(cols)):
##                n = cls.getFieldNo(cols[i])
##                val = obj.m_fieldVals[n]            
##                if (val == None):
##                    val = ''
##                if (type(val) != str and type(val) != unicode):
##                    val = str(val)
##                
##                listview.SetStringItem(row, i, val)    
##        
    #def refreshViewCustomerList(self):
    #    CListCtrlAider.refreshListCtrlData(self.listCtrl_customer, self.custCols, CDataCustomer)
        
    
    def OnFramePeerClose(self, event):
        self.Show(False)

    def OnBtnAddButton(self, event):
        dlg = DlgCustomerEditor.create(self)
        if dlg.ShowModal() == wx.ID_OK:
            self.lcCust.refresh()

    def OnBtnDelButton(self, event):
        item = self.lcCust.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        if (item == -1):
            dlg = wx.MessageDialog(self, u'请选择列表项目', u'提示',
                                    wx.OK|wx.ICON_ERROR)
            dlg.ShowModal()
            return

    def OnBtnEditButton(self, event):
        item = self.lcCust.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        if (item == -1):
            dlg = wx.MessageDialog(self, u'请选择列表项目', u'提示',
                                    wx.OK|wx.ICON_ERROR)
            dlg.ShowModal()
            return
        
        val = int(self.lcCust.GetItem(item).GetText())

        custObj = CDataCustomer.getFirstInstance({CDataCustomer.COL_ID:val})
        if (custObj is None):
            dlg = wx.MessageDialog(self, u'未找到客户', u'提示',
                                    wx.OK|wx.ICON_ERROR)
            dlg.ShowModal()
            return
        
        dlg = DlgCustomerEditor.create(self)
        dlg.setDataObj(custObj)
        if (dlg.ShowModal() == wx.ID_OK):
            #self.listCtrl_customer.refresh()
            
            #self.refreshViewCustomerList()
            self.lcCust.refresh()

    def OnBtnAddSupplierButton(self, event):
        import DlgEditorSupplier
        dlg = DlgEditorSupplier.create(self)
        if (dlg.ShowModal() == wx.ID_OK):
            self.lcSupplier.refresh()

    def OnBtnEditSupplierButton(self, event):
        import DlgEditorSupplier
        
        item = self.lcSupplier.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        if (item == -1):
            dlg = wx.MessageDialog(self, u'请选择列表项目', u'提示',
                                    wx.OK|wx.ICON_ERROR)
            dlg.ShowModal()
            return
        
        val = int(self.lcSupplier.GetItem(item).GetText())
        peerObj = CDataSupplier.getFirstInstance({CDataSupplier.COL_ID:val})
        
        dlg = DlgEditorSupplier.create(self)
        dlg.setDataObj(custObj)
        if (dlg.ShowModal() == wx.ID_OK):
            self.lcSupplier.refresh()
